package com.yiban.demo.Station.Controller;

import com.yiban.demo.LoginUser.Entity.LoginUserPO;
import com.yiban.demo.Global.Entity.ResponseEntity;
import com.yiban.demo.Station.Entity.NearByStation;
import com.yiban.demo.Station.Entity.Station;
import com.yiban.demo.Station.Service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Station")
public class StationController {

    private StationService stationService;

    @Autowired
    public StationController(StationService stationService) {
        this.stationService = stationService;
    }

    /**
     *
     * @param station 注册的站点实例
     * @param loginUserPO 当前用户（从session取得）
     * @return 是否成功
     */
    @PostMapping(value = {"/station","/register"})
    public ResponseEntity<Boolean> registerStation(@Validated @RequestBody Station station, @SessionAttribute("user")LoginUserPO loginUserPO){
        return stationService.registerStation(station, loginUserPO.getUserId()) ? ResponseEntity.Success(Boolean.TRUE) : ResponseEntity.Failure("申请失败");
    }

    /**
     *
     * @param stationId 通过的stationId
     * @return 是否成功
     */
    @PutMapping("/station/{stationId}")
    public ResponseEntity<Boolean> passRegister(@PathVariable Integer stationId){
        return stationService.passRegisterRequest(stationId) ? ResponseEntity.Success(Boolean.TRUE) : ResponseEntity.Failure("检查stationId");
    }

    /**
     * 获取还没通过的station
     * @param current 当前页数
     * @param size 每页个数
     * @return 还没通过的station集合
     */
    @GetMapping("/stations/unpassed/{current}/{size}")
    public ResponseEntity<List<Station>> getUnPass(@PathVariable Long current, @PathVariable Long size){
        return ResponseEntity.Success(stationService.getRegisterRequest(current, size));
    }

    /**
     *
     * @param longitudes 经度
     * @param latitudes 纬度
     * @return 最近的已经上线站点
     */
    @GetMapping("/nearbyStations/{longitudes}/{latitudes}")
    public ResponseEntity<List<NearByStation>> getNearBy(@PathVariable BigDecimal longitudes, @PathVariable BigDecimal latitudes){
        return ResponseEntity.Success(stationService.getNearStation(longitudes, latitudes)
                .orElse(new ArrayList<>()));
    }

    /**
     *
     * @return 获取已经上线的station
     */
    @GetMapping("/onlineStation")
    public ResponseEntity<List<Station>> getOnline(){
        return ResponseEntity.Success(stationService.getOnlineStations());
    }
}
